package com.park.utils.wx;

import cn.hutool.core.io.IoUtil;
import com.github.wxpay.sdk.WXPayConfig;
import com.park.comm.WxComm;
import org.springframework.core.io.ClassPathResource;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;


/**
 * @author ZhangHaiWen
 * @title: MiniWXPayConfig
 * @projectName park
 * @description: 自定义实现微信支付配置类
 * @date 2020/5/822:03
 */
public class MiniWXPayConfig implements WXPayConfig  {


    private byte[] certData;

    public MiniWXPayConfig(){
        InputStream certStream = null;
        try {
            ClassPathResource classPathResource = new ClassPathResource("apiclient_cert.p12");
            //获取文件流
            certStream = classPathResource.getInputStream();
            this.certData = IoUtil.readBytes(certStream);
            certStream.read(this.certData);
            certStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(certStream!=null){
                try {
                    certStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public String getAppID() {
        return WxComm.USER_APPID;
    }

    @Override
    public String getMchID() {
        return WxComm.WX_PAY_MCH_ID;
    }

    @Override
    public String getKey() {
        return WxComm.WX_APP_PAY_KEY;
    }

    @Override
    public InputStream getCertStream() {
        ByteArrayInputStream certBis = new ByteArrayInputStream(this.certData);
        return certBis;
    }

    @Override
    public int getHttpConnectTimeoutMs() {
        return 0;
    }

    @Override
    public int getHttpReadTimeoutMs() {
        return 0;
    }

    //枚举单例设计模式
    enum SingleWXPayConfig{
        INSTENCE;
        private MiniWXPayConfig miniWXPayConfig;

        SingleWXPayConfig(){
            miniWXPayConfig = new MiniWXPayConfig();
        }

        public MiniWXPayConfig getInstence(){
            return miniWXPayConfig;
        }

    }
}
